<template>
  <div class="gongshi">
    <el-dialog title="批示意见/办理提示" :visible.sync="noticeVisible" width="95%">
      <notice :noticePersonId="noticePersonId" :step="1"></notice>
      <div slot="footer" class="dialog-footer" style="overflow:hidden">
        <el-button @click.native="noticeVisible = false" style="float:right">关闭</el-button>
      </div>
    </el-dialog>
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">系统首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/selection' }">干部选任</el-breadcrumb-item>
      <el-breadcrumb-item>公示</el-breadcrumb-item>
    </el-breadcrumb>
    <section class="wboxnoborder">
      <div :style="'height:'+tableHeight+'px'">
        <el-scrollbar style="height: calc(100% + 16px);">
          <el-tabs v-model="activeName" type="card" @tab-click="tabsClick" style="margin:15px">
            <el-tab-pane label="待公示" name="first">
              <span slot="label" v-if="num1_all>0" class="pointtab"><el-badge :value="num1_all" class="item">待公示</el-badge></span>
              <el-radio-group v-model="radioGroup" @change="steponeRadioChange" style="margin:0 0 15px" class="pointRadio">
                <el-radio-button label="干部一处事项">
                  <el-badge v-if="num1_1>0" :value="num1_1" class="item">干部一处事项</el-badge>
                </el-radio-button>
                <el-radio-button label="干部二处事项">
                  <el-badge v-if="num1_2>0" :value="num1_2" class="item">干部二处事项</el-badge>
                </el-radio-button>
                <el-radio-button label="干部三处事项">
                  <el-badge v-if="num1_3>0" :value="num1_3" class="item">干部三处事项</el-badge>
                </el-radio-button>
              </el-radio-group>
              <el-table
                border
                size="small"
                :data="tableData"
                tooltip-effect="dark"
                style="width: 100%; margin-top: 15px;"
                @selection-change="handleSelectionChange"
                v-loading="listloading"
                >
                <el-table-column
                  type="selection"
                  width="45">
                </el-table-column>
                <el-table-column
                  label="序号"
                  type="index"
                  width="50">
                </el-table-column>
                <el-table-column
                  prop="realName"
                  label="姓名"
                  width="90"
                  show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                  prop="candidateUnit"
                  label="目前单位"
                  show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                  prop="currentJob"
                  label="目前职务"
                  width="150"
                  show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                  prop="currentLevel"
                  label="目前职级"
                  width="100"
                  show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                  prop="unitName"
                  label="选任岗位单位"
                  show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                  prop="jobName"
                  width="150"
                  label="选任岗位职务"
                  show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                  prop="jobLevel"
                  label="选任岗位职级"
                  width="110"
                  show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                  label="办理提示、批示意见"
                  width="150">
                  <template slot-scope="scope">
                    <el-popover placement="bottom" width="600" trigger="hover">
                      <notice :noticePersonId="scope.row.personId" step="8"></notice>
                      <el-button size="small" slot="reference" type="primary" @click="showNotice(scope.row.personId)">查看详情</el-button>
                    </el-popover>
                    <!-- <el-button @click="showNotice(scope.row.personId)" type="primary" size="small">查看详情</el-button> -->
                  </template>
                </el-table-column>
              </el-table>
              <el-form ref="form"
                :model="gongshiForm"
                label-width="120px"
                label-position="right"
                class="marginT15">
                <el-form-item label="公示时间">
                  <el-col :span="6">
                    <el-date-picker
                      v-model="gongshiForm.date"
                      type="daterange"
                      align="right"
                      unlink-panels
                      range-separator="至"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                      :picker-options="gongshiForm.pickerOptions"
                      value-format="yyyy-MM-dd">
                    </el-date-picker>
                  </el-col>
                </el-form-item>
                <el-form-item>
                  <el-col :span="3">
                    <el-button type="primary" @click="export2">生成公示模板</el-button>
                  </el-col>
                  <el-col :span="3">
                    <el-button type="primary" @click="export1">生成公示报告</el-button>
                  </el-col>
                </el-form-item>
                <el-form-item label="上传附件">
                  <el-col :span="12">
                    <el-upload
                      class="upload-demo"
                      :action="this.$http.defaults.baseURL+this.$http.defaults.selectionAPI +'api/gen/file/upload/'"
                      :on-success="handleUploadSucc"
                      :on-remove="handleRemove"
                      :before-remove="beforeRemove"
                      :on-error='uploadError'
                      :multiple="true"
                      :file-list="fileList">
                      <el-button type="primary">点击上传</el-button>
                    </el-upload>
                  </el-col>
                </el-form-item>
                <el-form-item>
                  <el-button type="success" @click="submitGongshi" :loading="submitGongshiLoading">确认公示</el-button>
                </el-form-item>
              </el-form>
            </el-tab-pane>
            <el-tab-pane label="公示中" name="second">
              <span slot="label" v-if="num2_all>0" class="pointtab"><el-badge :value="num2_all" class="item">公示中</el-badge></span>
              <el-radio-group v-model="radioGroup" @change="steponeRadioChange" style="margin:0 0 15px" class="pointRadio">
                <el-radio-button label="干部一处事项">
                  <el-badge v-if="num2_1>0" :value="num2_1" class="item">干部一处事项</el-badge>
                </el-radio-button>
                <el-radio-button label="干部二处事项">
                  <el-badge v-if="num2_2>0" :value="num2_2" class="item">干部二处事项</el-badge>
                </el-radio-button>
                <el-radio-button label="干部三处事项">
                  <el-badge v-if="num2_3>0" :value="num2_3" class="item">干部三处事项</el-badge>
                </el-radio-button>
              </el-radio-group>
              <el-table
                size="small"
                border
                :data="tableData2"
                tooltip-effect="dark"
                v-loading="listloading2"
                style="width: 100%; margin-top: 15px;">
                <el-table-column
                  type="index"
                  label="序号"
                  width="50">
                </el-table-column>
                <el-table-column
                  prop="names"
                  label="人选"
                  width="300"
                  show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                  prop="times"
                  label="公示时间"
                  width="250">
                </el-table-column>
                <el-table-column
                  prop="fujian"
                  label="附件">
                  <template slot-scope="scope">
                    <el-link
                      :href="$http.defaults.baseURL+$http.defaults.selectionAPI+'api/gen/file/download?id='+item.id"
                      type="primary"
                      v-for="(item, idx) of scope.row.files"
                      :key="idx"
                      style="margin-left:5px;">{{item.fileName}}.{{item.fileType}}</el-link>
                  </template>
                </el-table-column>
                <el-table-column
                  label="操作"
                  width="300">
                  <template slot-scope="scope">
                    <!-- <el-button @click="finishGs(scope.row)" type="primary" size="small">公示完成</el-button> -->
                    <el-button @click="delGspc(scope.row)" type="danger" size="small">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </el-tab-pane>
          </el-tabs>
        </el-scrollbar>
      </div>
    </section>
  </div>
</template>

<script>
import notice from '../common/Notice'
export default {
  data () {
    return {
      submitGongshiLoading: false,
      listloading: false,
      listloading2: false,
      tableHeight: 100,
      activeName: 'first',
      radioGroup: '干部一处事项',
      dgsData: [], // 待公示 选择公示批次人员信息
      files: [],
      fileList: [], // 待公示 表单附件
      gongshiForm: { // 待公示 表单信息
        date: '',
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近一个月',
            onClick (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }]
        }
      },
      tableData: [],
      tableData2: [],
      selApi: 'api/selection/publicity/submitApprove',
      selStep: 6,
      deptid: 1,
      num1_all: 0,
      num2_all: 0,
      num1_1: 0,
      num1_2: 0,
      num1_3: 0,
      num2_1: 0,
      num2_2: 0,
      num2_3: 0,
      noticePersonId: '',
      noticeVisible: false
    }
  },
  components: {
    notice
  },
  mounted () {
    // 待公示 一处列表
    this.getList(this.deptid)
    // resize
    this.tableresize(this)
    let _this = this
    window.onresize = function windowResize () {
      // 通过捕获系统的onresize事件触发我们需要执行的事件
      _this.tableresize(_this)
    }
  },
  methods: {
    showNotice (id) {
      this.noticePersonId = id
      this.noticeVisible = true
    },
    export1 () {
      let arr = [] // 人选Id
      for (let i of this.dgsData) {
        arr.push(i.personId)
      }
      let para = {
        dateStart: this.gongshiForm.date[0],
        dateEnd: this.gongshiForm.date[1],
        personIds: arr
      }
      this.$http
        .post(this.$http.defaults.selectionAPI + `api/selection/document/exportReportDoc`, para)
        .then(response => {
          if (response.data.success) {
            this.download(response.data.result)
          } else {
            this.$message({
              message: '生成失败:请先选择人选，填写公示时间，并确保讨论决定环节会议信息填写完整',
              type: 'error'
            })
          }
        })
        .catch(function (error) {
          console.log(error)
          this.$message({
            message: '生成失败:' + error,
            type: 'error'
          })
        })
    },
    export2 () {
      let arr = [] // 人选Id
      for (let i of this.dgsData) {
        arr.push(i.personId)
      }
      let para = {
        dateStart: this.gongshiForm.date[0],
        dateEnd: this.gongshiForm.date[1],
        personIds: arr
      }
      this.$http
        .post(this.$http.defaults.selectionAPI + `api/selection/document/exportNoticeDoc`, para)
        .then(response => {
          if (response.data.success) {
            this.download(response.data.result)
          } else {
            this.$message({
              message: '生成失败:请先选择人选，填写公示时间，并确保讨论决定环节会议信息填写完整',
              type: 'error'
            })
          }
        })
        .catch(function (error) {
          console.log(error)
          this.$message({
            message: '生成失败:' + error,
            type: 'error'
          })
        })
    },
    download (fileid) {
      var downloadElement = document.createElement('a')
      downloadElement.href = this.$http.defaults.baseURL + this.$http.defaults.selectionAPI + 'api/gen/file/download?id=' + fileid
      document.body.appendChild(downloadElement)
      downloadElement.click() // 点击下载
      document.body.removeChild(downloadElement) // 下载完成移除元素
    },
    tabsClick () { // tab切换
      if (this.activeName === 'first') {
        this.gongshiForm.date = ''
        this.files = []
        this.fileList = []
        this.getList(this.deptid)
      } else if (this.activeName === 'second') {
        this.getList2(this.deptid)
      }
    },
    // getList (dc) { // 待公示
    //   this.listloading = true
    //   let url = this.$http.defaults.selectionAPI + 'api/selection/publicity/getNotProcessedPersons'
    //   console.log(url)
    //   this.$http.get(url, {
    //     params: {
    //       departmentCode: dc,
    //       sended: 0
    //     }
    //   }).then(res => {
    //     this.listloading = false
    //     console.log('get list succ:', res)
    //     if (res.data.success === true) {
    //       this.tableData = res.data.result
    //     } else {
    //       this.$message({
    //         message: '数据获取失败',
    //         type: 'error'
    //       })
    //     }
    //   })
    // },
    getList2 (dc) { // 待公示
      this.getNums()
      this.listloading2 = true
      let url = this.$http.defaults.selectionAPI + 'api/selection/publicity/getProcessingPublicityBatchInfo'
      // console.log(url)
      this.$http.get(url, {
        params: {
          departmentCode: dc
        }
      }).then(res => {
        this.listloading2 = false
        console.log('get list2 succ:', res)
        if (res.data.success === true) {
          let rs = res.data.result
          let arr = []
          for (let i of rs) {
            let brr = []
            for (let j of i.persons) {
              brr.push(j.realName)
            }
            arr.push({
              names: brr.join(' '),
              times: i.dateStart + '至' + i.dateEnd,
              files: i.files,
              persons: i.persons,
              id: i.id
            })
          }
          console.log('tabledata2:', arr)
          this.tableData2 = arr
        } else {
          this.$message({
            message: '数据获取失败',
            type: 'error'
          })
        }
      })
    },
    getList (dc) {
      this.getNums()
      this.listloading = true
      let url = this.$http.defaults.selectionAPI + 'api/selection/publicity/getApprove'
      // console.log(url)
      this.$http.get(url, {
        params: {
          departmentCode: dc
        }
      }).then(res => {
        this.listloading = false
        console.log('get list4 succ:', res)
        if (res.data.success === true) {
          this.tableData = res.data.result
        } else {
          this.$message({
            message: '数据获取失败',
            type: 'error'
          })
        }
      })
    },
    steponeRadioChange () { // 待公示 radiogroup change
      if (this.radioGroup === '干部一处事项') {
        this.deptid = 1
      } else if (this.radioGroup === '干部二处事项') {
        this.deptid = 2
      } else if (this.radioGroup === '干部三处事项') {
        this.deptid = 3
      }
      this.tabsClick()
    },
    handleSelectionChange (val) { // 待公示 table select change
      console.log('tabledata select val:', val)
      this.dgsData = [...val]
    },
    handleUploadSucc (response, file, fileList) { // 待公示 表单提交 附件
      console.log(response)
      if (response.success) {
        this.files.push(response.result[0])
        this.$message({
          message: '上传成功！' + response.message,
          type: 'success'
        })
      } else {
        this.$message({
          message: '上传失败:' + response.message,
          type: 'error'
        })
      }
    },
    beforeRemove (file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    handleRemove (file, fileList) {
      console.log(file, fileList)
      let fid = file.response.result[0].id
      let url = this.$http.defaults.selectionAPI + 'api/gen/file/remove'
      this.$http.get(url, {
        params: {
          id: fid
        }
      }).then(res => {
        console.log('删除成功:', res)
        if (res.data.success === true) {
          let index = this.files.indexOf(fid)
          if (index !== -1) {
            this.files.splice(index, 1)
            this.$message({
              message: '删除成功！',
              type: 'success'
            })
          }
        }
      })
    },
    uploadError (err) {
      console.log(err)
      this.$message({
        message: '上传失败:' + err,
        type: 'error'
      })
    },
    submitGongshi () { // 待公示 公示表单提交
      console.log('gongshi form submit', this.gongshiForm)
      if (this.dgsData.length === 0) {
        this.$message({
          message: '请选择公示人',
          type: 'warning'
        })
        return
      }
      if (this.gongshiForm.date === '') {
        this.$message({
          message: '请选择公示时间',
          type: 'warning'
        })
        return
      }
      let arr = [] // 文件Id
      for (let i of this.files) {
        arr.push(i.id)
      }
      let brr = [] // 人选Id
      for (let i of this.dgsData) {
        brr.push(i.personId)
      }
      console.log(arr, brr)

      let url = this.$http.defaults.selectionAPI + 'api/selection/publicity/savePublicityBatchByPersons'

      // 增加判断公示结束日期是否在当前时间之前，如果是需要确认才提交，否则不用确认即可提交

      let mydate = new Date()
      let nowdate = new Date(mydate.getFullYear() + '-' + (mydate.getMonth() + 1) + '-' + mydate.getDate())
      let dateEnd = new Date(this.gongshiForm.date[1])
      console.log(nowdate + '|' + dateEnd)
      console.log(nowdate > dateEnd)
      if (dateEnd < nowdate) {
        this.$confirm('您填写的公示结束时间在当前时间之前，所选人选会直接完成公示，是否确认公示？', '提示', {
          confirmButtonText: '是',
          cancelButtonText: '否',
          type: 'warning'
        }).then(() => {
          this.submitGongshiLoading = true
          this.$http.post(url, {
            dateStart: this.gongshiForm.date[0],
            dateEnd: this.gongshiForm.date[1],
            files: arr,
            personIds: brr
          }).then(res => {
            this.submitGongshiLoading = false
            console.log('待公示表单提交res:', res)
            if (res.data.success === true) {
              this.$message({
                message: '确认公示成功',
                type: 'success'
              })
              this.getList(this.deptid)
            }
          })
        })
      } else {
        this.submitGongshiLoading = true
        this.$http.post(url, {
          dateStart: this.gongshiForm.date[0],
          dateEnd: this.gongshiForm.date[1],
          files: arr,
          personIds: brr
        }).then(res => {
          this.submitGongshiLoading = false
          console.log('待公示表单提交res:', res)
          if (res.data.success === true) {
            this.$message({
              message: '确认公示成功',
              type: 'success'
            })
            this.getList(this.deptid)
          }
        })
      }
    },
    finishGs (item) { // 公示中 公示完成
      console.log('公示中 公示完成:', item)
      let url = this.$http.defaults.selectionAPI + 'api/selection/publicity/updatePublicityComplete'
      // console.log(url)
      this.$http.get(url, {
        params: {
          publicityBatchId: item.id
        }
      }).then(res => {
        console.log('完成公示res:', res)
        if (res.data.success === true) {
          this.$message({
            message: '公示完成',
            type: 'success'
          })
          this.getList2(this.deptid)
        }
      })
    },
    delGspc (item) { // 公示中 删除 批次
      console.log('删除公示中批次:', item)
      this.$confirm('是否删除该公示批次', '操作确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = this.$http.defaults.selectionAPI + 'api/selection/publicity/removePublicityBatch'
        this.$http.get(url, {
          params: {
            publicityBatchId: item.id
          }
        }).then(res => {
          console.log('删除公示中批次res:', res)
          if (res.data.success === true) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.getList2(this.deptid)
          }
        })
      }).catch(() => {
        console.log('取消删除')
      })
    },
    tableresize (obj) {
      var clientHeight = 0
      if (document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      } else {
        clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      }
      obj.$nextTick(function () {
        obj.tableHeight = clientHeight - 145
      })
    },
    getNums () {
      this.$http
        .get(this.$http.defaults.selectionAPI + `api/selection/statnum/publicity/getStatNum3`)
        .then(response => {
          if (response.data.success) {
            this.num1_1 = response.data.result[0][1]
            this.num1_2 = response.data.result[0][2]
            this.num1_3 = response.data.result[0][3]
            this.num1_all = response.data.result[0][0]
            this.num2_1 = response.data.result[1][1]
            this.num2_2 = response.data.result[1][2]
            this.num2_3 = response.data.result[1][3]
            this.num2_all = response.data.result[1][0]
          } else {
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(function (error) {
          console.log(error)
        })
    }
  }
}
</script>
<style>
.gongshi .el-table--border td, .el-table--border th, .el-table__body-wrapper .el-table--border.is-scrolling-left~.el-table__fixed{
  border-bottom:1px solid #95B8E7;
}
.gongshi .el-table td, .el-table th.is-leaf{
  border-right:1px solid #95B8E7;
}
.gongshi .wboxnoborder .el-table{
  border: #95B8E7 solid 1px;
}
.gongshi .wboxnoborder .el-table th{
  font-weight: normal;
  background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
  text-align: center;
  border-right: #95B8E7 solid 1px;
  border-bottom: #95B8E7 solid 1px;
  min-height: 30px;
  line-height:30px;
}
.gongshi .wboxnoborder .el-table td, .el-table th{
  padding:0;
  font-size:var(--size14) !important;
  line-height: 20px;
  font-family: '宋体';
  text-align: center;
}
.gongshi .el-table__row{
  height: 50px;
}
</style>
<style scoped>
.gongshi {
  width: 100%;
  box-sizing: border-box;
}
.gongshi .wboxnoborder {
  padding: 0;
  width: 100%;
  background-color: #fff;
  margin: 0 10px;
  overflow: hidden;
}
</style>
